#include "JanusPeerConnection.h"
#include "Signaling.h"

JanusPeerConnection::JanusPeerConnection(Signaling *pSignalHelper, const QString &strOpaqueID, QObject *parent)
    : QObject(parent)
{
    m_strOpaqueID = strOpaqueID;
    m_handleID = 0;
    m_privateID = 0;
    m_subscribeID = 0;
    m_pSignalHelper = pSignalHelper;
}

JanusPeerConnection::~JanusPeerConnection()
{

}

void JanusPeerConnection::attachVideoRoom()
{
    QJsonObject msg;
    msg["janus"] = "attach";

    msg["plugin"] = "janus.plugin.videoroom";

    msg["opaque_id"] = m_strOpaqueID;

    m_pSignalHelper->sendMsg(msg, std::bind(&JanusPeerConnection::onAttachVideoRoom,
                                            this, std::placeholders::_1));
}

void JanusPeerConnection::onAttachVideoRoom(const QJsonObject &recvData)
{
    if(recvData.contains("id"))
    {
        m_handleID = recvData.value("id").toDouble();

        QJsonObject msg;
        msg[KEY_JANUS] = "message";
        msg["handle_id"] = m_handleID;

        QJsonObject body;
        body["request"] = "join";
        body["room"] = 1234;
        if (m_subscribeID == 0)
        {
            body["ptype"] = "publisher";
            body["display"] = "zhangmingming";
        }
        else
        {
            //"body":{"request":"join","room":1234,"ptype":"subscriber","feed":8452751313644181,"private_id":2775113861}
            body["ptype"] = "subscriber";
            body["feed"] = m_subscribeID;
            body["private_id"] = m_privateID;
        }

        msg["body"] = body;

        m_pSignalHelper->sendMsg(msg);
    }
}

void JanusPeerConnection::sendSDP(const QString &strSDP, const QString &strType)
{
    QJsonObject msg;
    msg[KEY_JANUS] = "message";

    QJsonObject body;
    if (m_subscribeID == 0)
    {
        body["request"] = "configure";
        body["audio"] = true;
        body["video"] = true;
    }
    else
    {
        body["request"] = "start";
        body["room"] = 1234;
    }
    msg["body"] = body;

    QJsonObject jsep;
    jsep["type"] = strType;
    jsep["sdp"] = strSDP;
    msg["jsep"] = jsep;

    msg["handle_id"] = m_handleID;
    m_pSignalHelper->sendMsg(msg, std::bind(&JanusPeerConnection::onSendSDP, this, std::placeholders::_1));
//    Signaling::instance()->sendMsg(msg);
}

void JanusPeerConnection::onSendSDP(const QJsonObject &recvData)
{
    qDebug() << __func__ << recvData;
}

void JanusPeerConnection::sendCandidate(const QString &strSDPMid, const int &iSDPMLineIndex, const QString &strCandidate)
{
    QJsonObject msg;
    msg[KEY_JANUS] = "trickle";

    QJsonObject candidateObj;
    if (strSDPMid == "end")
    {
        candidateObj["completed"] = true;
    }
    else
    {
        candidateObj["candidate"] = strCandidate;
        candidateObj["sdpMid"] = strSDPMid;
        candidateObj["sdpMLineIndex"] = iSDPMLineIndex;
    }
    msg["candidate"] = candidateObj;

    msg["handle_id"] = m_handleID;
    m_pSignalHelper->sendMsg(msg, std::bind(&JanusPeerConnection::onSendCandidate,
                                            this, std::placeholders::_1));
}

void JanusPeerConnection::onSendCandidate(const QJsonObject &recvData)
{
    qDebug() << __func__ << recvData;
}

qint64 JanusPeerConnection::getHandleID()
{
    return m_handleID;
}

bool JanusPeerConnection::isSubscribe()
{
    return m_subscribeID != 0;
}

void JanusPeerConnection::setSubscribe(const qint16 &id)
{
    m_subscribeID = id;
}

qint64 JanusPeerConnection::getSubscribe()
{
    return  m_subscribeID;
}

void JanusPeerConnection::setPrivateID(const qint64 &id)
{
    m_privateID = id;
}

void JanusPeerConnection::detachVideoRoom()
{
    QJsonObject msg;
    msg[KEY_JANUS] = "detach";
    msg["handle_id"] = m_handleID;
    m_pSignalHelper->sendMsg(msg, std::bind(&JanusPeerConnection::onDetachVideoRoom,
                                            this, std::placeholders::_1));
}

void JanusPeerConnection::onDetachVideoRoom(const QJsonObject &recvData)
{
    qDebug() << recvData;
}
